﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

//使用别名 
using MyGeneric = Collections.Grngric.泛型委托.GenericDelegate;
using MyCollectionGeneric = Collections.Grngric.别名.objects<string>;

public partial class 泛类型 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Collections.Grngric.继承使用.GenericClassTest<DateTime> c = new Collections.Grngric.继承使用.GenericClassTest<DateTime>();
        Response.Write(c.Output(DateTime.Now) + "<br />");

        Collections.Grngric.继承使用.GenericClassFrom c1 = new Collections.Grngric.继承使用.GenericClassFrom();
        Response.Write(c1.Output("abc") + "<br />");

        Collections.Grngric.继承使用.GenericClassTest<Guid> c2 = new Collections.Grngric.继承使用.InheritClass2<Guid>();
        Response.Write(c2.Output(new Guid()) + "<br />");

        Collections.Grngric.继承使用.InheritClass3<int> c3 = new Collections.Grngric.继承使用.InheritClass3<int>();
        Response.Write(c3.Output(Math.PI) + " ||| " + c3.ChildOutput(123) + "<br />");

        泛类型 oo = new 泛类型();

        ///使用型的委托
        UseDelegateforGeneric();
    }


    /// <summary>
    /// 使用型的委托
    /// </summary>
    void UseDelegateforGeneric()
    {
        Collections.Grngric.泛型委托.GenericDelegate oo = new Collections.Grngric.泛型委托.GenericDelegate();

       MyGeneric.OutputDelegate<string> delegate1 = new MyGeneric.OutputDelegate<string>(MyGeneric.DelegateFun);

       Response.Write(delegate1(Guid.NewGuid().ToString()));

       // 使用泛型委托（匿名方法）
       Collections.Grngric.泛型委托.GenericDelegate.OutputDelegate<DateTime> delegate2 = Collections.Grngric.泛型委托.GenericDelegate.DelegateFun;
       Response.Write(delegate2(DateTime.Now));

        ///抽象类
       Collections.Grngric.抽象泛型类派生约束.泛类型111 泛类型1112 = new Collections.Grngric.抽象泛型类派生约束.泛类型111();
       泛类型1112.OnLad();

        Collections.Grngric.接口派生约束构造函数约束.Generic_Interface GenericClass = new Collections.Grngric.接口派生约束构造函数约束.Generic_Interface();
        GenericClass.ppage_Load();

        ///别名
        MyCollectionGeneric objects = new Collections.Grngric.别名.objects<string>();

    }

}


namespace Collections.Grngric.继承使用
{
    public static class 泛類型的靜態方法
    { 
        /// <summary>
        /// 靜謐 new 的方法
        /// </summary>
        /// <typeparam name="T">類型</typeparam>
        /// <returns>返回類型</returns>
        public  static T Test<T>() where T :new ()
        {
            return new T();
        }

        /// <summary>
        ///
        /// 一次使用兩 兩個泛類型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="Y"></typeparam>
        /// <returns></returns>
        public static T 兩個泛類型<T, Y>()
            where T : new()
            where Y : new() 
            where Y:T
        {


            return (T)new Y();
        }
    }
}

//strat 泛类型
/// 如何 使用 和 继承 泛型类
namespace Collections.Grngric.继承使用
{
    ///添加泛类型的引用
    using System.Collections.Generic;//

    /// <summary>
    /// // 泛型类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class GenericClassTest<T>
    {
        public virtual string Output(T t)
        {
            return "类型:" + t.GetType() + "值:" + t.ToString();
        }
    }

    /// <summary>
    /// 继承自一个泛型类，指定基类的泛型的具体类型
    /// </summary>
    public class GenericClassFrom : GenericClassTest<string>
    {


    }

    /// <summary>
    ///  继承自一个泛型类，基类和子类都是同一泛型
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class InheritClass2<T> : GenericClassTest<T>
    {
        public override string Output(T t)
        {
            return "子类实现-类型：" + t.GetType().ToString() + "；值：" + t.ToString();
        }
    }

    // 继承自一个泛型类，指定基类的泛型的具体类型，子类仍然可以是泛型
    public class InheritClass3<Z> : GenericClassTest<double>
    {
        public string ChildOutput(Z z)
        {
            return "子类实现-类型：" + z.GetType().ToString() + "；值：" + z.ToString();
        }
    }
}

namespace Collections.Grngric.泛类型的方法
{
    using System.Collections.Generic;

    /// <summary>
    /// 泛类型的方法
    /// </summary>
    public class GenericMethod
    {
        /// <summary>
        ///  静态 泛型方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>

        public static string Output<T>(T t)
        {
            return "类型：" + t.GetType().ToString() + "；值：" + t.ToString();
        }
    }
}

///如何 使用 泛型委托
namespace Collections.Grngric.泛型委托
{
    using System.Collections.Generic;

    /// <summary>
    /// 如何 使用 泛型委托
    /// </summary>
    public class GenericDelegate
    {
        // 声明一个泛型委托
        public delegate string OutputDelegate<T>(T t);

        // 定义一个静态方法
        public static string DelegateFun(string s)
        {
            return String.Format("Hello, {0}", s);
        }

        // 定义一个静态方法
        public static string DelegateFun(DateTime dt)
        {
            return String.Format("Time, {0}", dt.ToString());
        }


    }
}




///抽象泛型类派生约束
namespace Collections.Grngric.抽象泛型类派生约束
{
    /*---
    * 如何 使用 和 继承 抽象泛型类 
    * 派生约束
    ---*/
    using System;
    using System.Collections.Generic;

    // 泛型抽象类
    public abstract class GenericParent
    {
        /// <summary>
        /// 泛型抽象方法，返回值为一个泛型，加一个约束使泛型X要继承自泛型Y
        /// </summary>
        /// <typeparam name="X"></typeparam>
        /// <typeparam name="Y"></typeparam>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public abstract X Output<X, Y>(X x, Y y) where X : Y;

        /// <summary>
        /// 泛型抽象方法，返回值为一个string类型，加一个约束使泛型X要继承自泛型Y
        /// </summary>
        /// <typeparam name="X"></typeparam>
        /// <param name="x"></param>
        /// <returns></returns>
        public abstract string Output2<X>(X x) where X : System.ComponentModel.IListSource;
    }

    /// <summary>
    /// 新建抽象类型
    /// </summary>
    public interface  GenericPartent
    {
          string output(string l);
    }
    public class GenericChild : GenericParent, GenericPartent
    {
        /// <summary>
        /// 重写抽象类的泛型方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="Z"></typeparam>
        /// <param name="t"></param>
        /// <param name="z"></param>
        /// <returns></returns>
        public override T Output<T, Z>(T t, Z z)
        {
            return t;
        }

        /// <summary>
        ///  重写抽象类的泛型方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public override string Output2<T>(T t)
        {
            return t.GetType().ToString();
        }
        
        /// <summary>
        /// 实现接口
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public  string output(string o)
        {
            return o+"o";
        }
    }


    public partial class 泛类型111 : System.Web.UI.Page
    {
        public void OnLad()
        {
           

            GenericChild gc = new GenericChild();
           System.Web.HttpContext.Current. Response.Write(gc.Output<string, IComparable>("aaa", "xxx"));
           System.Web.HttpContext.Current.Response.Write("<br />");

           System.Web.HttpContext.Current.Response.Write(gc.Output2<System.Data.DataTable>(new System.Data.DataTable()));
           System.Web.HttpContext.Current.Response.Write("<br />");

           System.Web.HttpContext.Current.Response.Write(gc.output("sdlkfklsdjflksdjlkfsd"));
        }
    }
}


///泛型接口，派生约束，构造函数约束
namespace Collections.Grngric.接口派生约束构造函数约束
{
    using System.Collections.Generic;
    
    /// <summary>
    /// // 泛型接口
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public interface IGenericInterface<T>
    {
        T CreateInstance(); 
    }

    // 实现上面泛型接口的泛型类
    // 派生约束where T : TI（T要继承自TI）
    // 构造函数约束where T : new()（T可以实例化）
    public class Factory<T, TI> : IGenericInterface<TI> where T : TI, new()
    {
        public TI CreateInstance()
        {
            return new T();
        }
    }


    public partial class Generic_Interface : System.Web.UI.Page
    {
        public void ppage_Load()
        {
            IGenericInterface<System.ComponentModel.IListSource> factory =  new Factory<System.Data.DataTable, System.ComponentModel.IListSource>();

            System.Web.HttpContext.Current.Response.Write(factory.CreateInstance().GetType().ToString());
            System.Web.HttpContext.Current.Response.Write("<br />");
        }
    }

}


///泛类型使用别名
namespace Collections.Grngric.别名
{
    
    public class objects<T>
    { 
        
    }
}